package com.games.fifteenpieces;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

public class Piece {
	
	
	private Drawable mPic;
	private Context mContext;
	private static int HEIGHT, WIDTH;
	private int mRealI, mRealJ, mCorrectI, mCorrectJ;
	
	/*
	 * The only constructror for piece.
	 * 
	 * @param correctPlace place on field (coordinates in 2D-array), which considering as correct
	 * 
	 */
	public Piece(Context context, Bundle correctPlace){
		mContext = context;
		mCorrectI = correctPlace.getInt(Field.COORD_X);
		mCorrectJ = correctPlace.getInt(Field.COORD_Y);
		
		//Just workaround
		int num = correctPlace.getInt(Field.COORD_Y) * Field.PIECES_HORIZONTAL_NUMBER +
															correctPlace.getInt(Field.COORD_X) + 1; 
		switch (num) {
		case 1:
			mPic = mContext.getResources().getDrawable(R.drawable.piece_1);
			break;
		case 2:
			mPic = mContext.getResources().getDrawable(R.drawable.piece_2);
			break;
		case 3:
			mPic = mContext.getResources().getDrawable(R.drawable.piece_3);
			break;
		case 4:
			mPic = mContext.getResources().getDrawable(R.drawable.piece_4);
			break;
		case 5:
			mPic = mContext.getResources().getDrawable(R.drawable.piece_5);
			break;
		case 6:
			mPic = mContext.getResources().getDrawable(R.drawable.piece_6);
			break;
		case 7:
			mPic = mContext.getResources().getDrawable(R.drawable.piece_7);
			break;
		case 8:
			mPic = mContext.getResources().getDrawable(R.drawable.piece_8);
			break;
		case 9:
			mPic = mContext.getResources().getDrawable(R.drawable.piece_9);
			break;
		case 10:
			mPic = mContext.getResources().getDrawable(R.drawable.piece_10);
			break;
		case 11:
			mPic = mContext.getResources().getDrawable(R.drawable.piece_11);
			break;
		case 12:
			mPic = mContext.getResources().getDrawable(R.drawable.piece_12);
			break;
		case 13:
			mPic = mContext.getResources().getDrawable(R.drawable.piece_13);
			break;
		case 14:
			mPic = mContext.getResources().getDrawable(R.drawable.piece_14);
			break;
		case 15:
			mPic = mContext.getResources().getDrawable(R.drawable.piece_15);
			break;
		//should never go to default 
		default:
			mPic = mContext.getResources().getDrawable(R.drawable.piece_back);
			break;			
		}
		HEIGHT = mPic.getIntrinsicHeight();
		WIDTH = mPic.getIntrinsicWidth();
		setRealPlace(correctPlace);
		}
	
	public void draw(Canvas c){
		mPic.draw(c);
		/*
		//some debug extra code
		Paint p = new Paint();
		p.setARGB(255, 255, 255, 255);
		p.setTextSize(20);
		String s = "c - " + mCorrectI + ":" + mCorrectJ;
		c.drawText(s, mPic.getBounds().left + 10, mPic.getBounds().top + 20, p);
		s = "r - " + mRealI + ":" + mRealJ;
		c.drawText(s, mPic.getBounds().left + 10, mPic.getBounds().top + 45, p);
		//end of extra code
		 * 
		 */
	}
	
	/**
	 * @return height of piece, calculated in constructor 
	 */
	
	public static int getHeight(){
		//Rect r = mPic.getBounds();
		//return r.bottom - r.top;
		//return mPic.getIntrinsicHeight();
		return HEIGHT;
	}

	/**
	 * @return width of piece, calculated in constructor 
	 */
		public static int getWidth(){
		//Rect r = mPic.getBounds();
		//return r.right - r.left;
		//return mPic.getIntrinsicWidth();
		return WIDTH;
	}
	
	/**
	 * Sets the realtime piece`s coordinates, resets bounds of picture relative to Field 
	 */
	public void setRealPlace(Bundle place){
		mRealI = place.getInt(Field.COORD_X);
		mRealJ = place.getInt(Field.COORD_Y);
		int w = getWidth(), h = getHeight();
		mPic.setBounds(w * mRealI, h * mRealJ, w * (mRealI + 1), h * (mRealJ + 1));
	}
	
	/**
	 * @return realtime coordinates of piece
	 */
	public Bundle getRealPlace(){
		Bundle realPlace = new Bundle();
		realPlace.putInt(Field.COORD_X, mRealI);
		realPlace.putInt(Field.COORD_Y, mRealJ);
		return realPlace;
	}

	public Bundle getCorrectPlace(){
		Bundle correctPlace = new Bundle();
		correctPlace.putInt(Field.COORD_X, mCorrectI);
		correctPlace.putInt(Field.COORD_Y, mCorrectJ);
		return correctPlace;
	}

	public boolean isOnCorrectPlace(){
		return ((mCorrectI == mRealI) && (mCorrectJ == mRealJ));
	}

	@Override
	public String toString() {
		return "Piece [CI=" + mCorrectI + ", CJ=" + mCorrectJ
				+ ", RI=" + mRealI + ", RJ=" + mRealJ + "]";
	}
}
